import 'package:flutter/material.dart';
import 'views/collection_page.dart';
import 'views/first_page.dart';
import 'views/fourth_page.dart';
import 'views/widget_page.dart';

void main() {
  runApp(MyApp());
}

const int ThemeColor = 0xFFC91B3A;

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Flutter Go',
      theme: ThemeData(
        primaryColor: Color(ThemeColor),
        backgroundColor: Color(0xFFEFEFEF),
        accentColor: Color(0xFF888888),
        textTheme: TextTheme(
          body1: TextStyle(color: Color(0xFF888888), fontSize: 16.0),
        ),
        iconTheme: IconThemeData(
          color: Color(ThemeColor),
          size: 35.0,
        ),
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  TabController controller;
  String appBarTitle = tabData[0]['text'];
  static List tabData = [
    {
      'view': new FirstPage(),
      'text': '业界动态',
      'icon': new Icon(Icons.language),
    },
    {
      'view': new WidgetPage(),
      'text': '组件',
      'icon': new Icon(Icons.extension),
    },
    {
      'view': new CollectionPage(),
      'text': '组件收藏',
      'icon': new Icon(Icons.star)
    },
    {
      'view': new FourthPage(),
      'text': '关于手册',
      'icon': new Icon(Icons.favorite),
    }
  ];
  List<Widget> myTabs = [];
  List<Widget> myTabBarViews = [];

  @override
  void initState() {
    super.initState();
    controller = new TabController(length: 4, vsync: this);
    for (int i = 0; i < tabData.length; i++) {
      myTabs.add(new Tab(text: tabData[i]['text'], icon: tabData[i]['icon']));
      myTabBarViews.add(tabData[i]['view']);
    }
    controller.addListener(() {
      //不注释则滑动不会触发
//      if (controller.indexIsChanging) {
      _onTabChange();
//      }
    });
  }

  void _onTabChange() {
    if (this.mounted) {
      this.setState(() {
        appBarTitle = tabData[controller.index]['text'];
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text(appBarTitle),
      ),
      body: new TabBarView(
        children: myTabBarViews,
        controller: controller, //滑动触发一次监听
      ),
      bottomNavigationBar: Material(
        child: SafeArea(
          child: Container(
            height: 65.0,
            decoration: BoxDecoration(
              color: const Color(0xFFF0F0F0),
              boxShadow: <BoxShadow>[
                BoxShadow(
                  color: const Color(0xFFd0d0d0),
                  blurRadius: 3.0,
                  spreadRadius: 2.0,
                  offset: Offset(-0.1, -0.1),
                ),
              ],
            ),
            child: TabBar(
              indicatorColor: Theme.of(context).primaryColor,
              indicatorWeight: 3.0,
              labelColor: Theme.of(context).primaryColor,
              unselectedLabelColor: const Color(0xFF8E8E8E),
              tabs: myTabs,
              controller: controller, //点击触发两次监听
            ),
          ),
        ),
      ),
    );
  }
}
